﻿namespace GuiaTest
{
    using System;
    using Guia.Core;
    using Guia.Elements;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using WpfTestApplication;

    /// <summary>
    /// Summary description for CustomControlTest
    /// </summary>
    [TestClass]
    public class CustomControlTest : AbstractTest
    {
        private CustomControl customControl;

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        [TestInitialize()]
        public void MyTestInitialize()
        {
            
        }

        // Use TestCleanup to run code after each test has run
        [TestCleanup()]
        public void MyTestCleanup()
        {
            if (customControl != null)
            {
                try
                {
                    customControl.Stop(true);
                }
                catch(ArgumentNullException)
                {
                    //can happen due to not correct initialized controls
                }
            }
        }

        #endregion

        [TestMethod]
        public void TestStart()
        {
            customControl = CustomControl.Start<SimpleUserControl>();
            Assert.IsNotNull(customControl);

            Assert.AreEqual("Label", customControl.Get<Label>("label1").TextValue);
            Assert.AreEqual("Label", customControl.Get<Label>("label2").TextValue);
            Assert.AreEqual("Label", customControl.Get<Label>("label3").TextValue);
        }

        [TestMethod]
        public void TestStartWithConstructorParameter()
        {
            customControl = CustomControl.Start<SimpleUserControl>("Show this","And this");
            Assert.IsNotNull(customControl);

            Assert.AreEqual("Show this", customControl.Get<Label>("label1").TextValue);
            Assert.AreEqual("And this", customControl.Get<Label>("label2").TextValue);
            Assert.AreEqual("Label", customControl.Get<Label>("label3").TextValue);
        }

        [TestMethod]
        public void TestStartWithInitializeAction()
        {
            customControl = CustomControl.Start<SimpleUserControl>((control) => control.Initialize("Initialized"));
            Assert.IsNotNull(customControl);

            Assert.AreEqual("Label", customControl.Get<Label>("label1").TextValue);
            Assert.AreEqual("Label", customControl.Get<Label>("label2").TextValue);
            Assert.AreEqual("Initialized", customControl.Get<Label>("label3").TextValue);
        }

        [TestMethod]
        public void TestCloseCustomControlWithModalDialog()
        {
            customControl = CustomControl.Start<SimpleUserControl>();
            customControl.Get<Button>("btnMsgBox").Invoke();
            // dialog stays open
            Delay.ForSeconds(0.5);
            Assert.IsTrue(true);
        }

        [TestMethod]
        public void TestStartWithInitializeActionAndConstructorParameter()
        {
            customControl = CustomControl.Start<SimpleUserControl>((control) => control.Initialize("Initialized"),"Show this", "And this");
            Assert.IsNotNull(customControl);

            Assert.AreEqual("Show this", customControl.Get<Label>("label1").TextValue);
            Assert.AreEqual("And this", customControl.Get<Label>("label2").TextValue);
            Assert.AreEqual("Initialized", customControl.Get<Label>("label3").TextValue);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestStartWithInitializeActionAndConstructorParameterActionNull()
        {
            customControl = CustomControl.Start<SimpleUserControl>(null, "Show this", "And this");
        }

        [TestMethod, ExpectedException(typeof(NoConstructorFoundException))]
        public void TestStartWithInvalidConstructorParameter()
        {
            customControl = CustomControl.Start<SimpleUserControl>((control) => control.Initialize("Initialized"), "Show this", "And this", "And also this");
            Assert.IsNotNull(customControl);
        }
    }
}
